package com.atguigu.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.atguigu.base.BaseDAO;
import com.atguigu.base.BaseServiceImpl;
import com.atguigu.dao.UserFollowDAO;
import com.atguigu.entity.UserFollow;
import com.atguigu.service.UserFollowService;
import com.atguigu.vo.UserFollowVo;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * @PACKAGE_NAME: com.atguigu.service.impl
 * @CLASSNAME: UserFollowServiceImpl
 * @AUTHOR: zhangsan
 * @DATE: 2024/4/29 15:55
 * @SINCE 17.0.7
 * @DESCRIPTION: UserFollowServiceImpl
 */
@Service(interfaceClass = UserFollowService.class)
@Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
public class UserFollowServiceImpl extends BaseServiceImpl<UserFollow> implements UserFollowService {
    @Resource
    private UserFollowDAO userFollowDAO;

    @Override
    protected BaseDAO<UserFollow> getEntityDAO() {
        return userFollowDAO;
    }

    /**
     * 根据用户id,分页查询该用户的关注列表
     *
     * @param pageNum
     * @param pageSize
     * @param id
     * @return
     */
    @Override
    public PageInfo<UserFollowVo> findListPage(Integer pageNum, Integer pageSize, Long userId) {
        PageHelper.startPage(pageNum, pageSize);
        Page<UserFollowVo> page = userFollowDAO.findListPage(userId);
        PageInfo<UserFollowVo> pageInfo = new PageInfo<>(page, 5);
        return pageInfo;
    }

    /**
     * 关注房源
     *
     * @param userId
     * @param houseId
     */
    @Override
    public UserFollow follow(Long userId, Long houseId) {
        //查一下之前是否关注过,如果是取消关注状态,则重新关注即可,
        //即将is_deleted = 0 变为 is_deleted= 1
        UserFollow userFollow = userFollowDAO.getByUserIdAndHouseId(userId, houseId);
        if (null == userFollow) {
            //如果之前没有关注过,则新建一个关注
            userFollow = new UserFollow();
            userFollow.setId(null);
            userFollow.setUserId(userId);
            userFollow.setHouseId(houseId);
            Integer i = userFollowDAO.insert(userFollow);
        } else {
            //说明之前关注过,处于取消关注状态
            Integer isDeleted = userFollow.getIsDeleted();
            if (isDeleted == 1) {
                userFollow.setIsDeleted(0);
                Integer update = userFollowDAO.updateIsDeleted(userFollow);
            }
        }
        return userFollow;
    }

    @Override
    public Boolean isFollowed(Long userId, Long houseId) {
        //通过该userId和houseId查询是否存在记录
        Integer count = userFollowDAO.countByUserIdAndHouseId(userId, houseId);
        if (count != null && count.intValue() == 0) {
            return false;
        }
        return true;
    }

    @Override
    public void cancelFollow(Long id) {
        Integer delete = userFollowDAO.delete(id);
    }

    @Override
    public void cancelFollow(Long userId, Long houseId) {
        UserFollow userFollow = userFollowDAO.getByUserIdAndHouseId(userId, houseId);
        Long id = userFollow.getId();
        Integer delete = userFollowDAO.delete(id);
    }
}
